package com.cci.kangdao.fieldService.service.impl;

import com.cci.kangdao.dao.UserTDao;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.fieldService.aop.model.CFWOThreadLocal;
import com.cci.kangdao.fieldService.dao.FieldServiceEngineerDao;
import com.cci.kangdao.fieldService.dao.FieldServiceEvaluationDao;
import com.cci.kangdao.fieldService.dao.FieldServiceItemDao;
import com.cci.kangdao.fieldService.dao.FieldServiceTemplateDao;
import com.cci.kangdao.fieldService.dao.FieldServiceTemplateParamDao;
import com.cci.kangdao.fieldService.dao.FieldServiceTemplatePickParamDao;
import com.cci.kangdao.fieldService.dao.FieldServiceTemplateSectionDao;
import com.cci.kangdao.fieldService.dao.FieldServiceTemplateStandardDao;
import com.cci.kangdao.fieldService.dao.FieldServiceUpgradeDao;
import com.cci.kangdao.fieldService.dao.FieldServiceWorkOrderDao;
import com.cci.kangdao.fieldService.model.FieldServiceEngineer;
import com.cci.kangdao.fieldService.model.FieldServiceEvaluation;
import com.cci.kangdao.fieldService.model.FieldServiceItem;
import com.cci.kangdao.fieldService.model.FieldServiceTemplate;
import com.cci.kangdao.fieldService.model.FieldServiceTemplateParam;
import com.cci.kangdao.fieldService.model.FieldServiceTemplatePickParam;
import com.cci.kangdao.fieldService.model.FieldServiceTemplateSection;
import com.cci.kangdao.fieldService.model.FieldServiceTemplateStandard;
import com.cci.kangdao.fieldService.model.FieldServiceUpgrade;
import com.cci.kangdao.fieldService.model.FieldServiceWorkOrder;
import com.cci.kangdao.fieldService.model.SyncFieldServiceOrderStatus;
import com.cci.kangdao.fieldService.model.type.FieldServiceDisplay;
import com.cci.kangdao.fieldService.model.type.FieldServiceItemStatus;
import com.cci.kangdao.fieldService.model.type.FieldServiceItemType;
import com.cci.kangdao.fieldService.model.type.FieldServiceRequestStatus;
import com.cci.kangdao.fieldService.model.type.FieldServiceUpgradeStatus;
import com.cci.kangdao.fieldService.model.type.FieldServiceWorkOrderStatus;
import com.cci.kangdao.fieldService.model.type.WorkOrderFlowProcessorType;
import com.cci.kangdao.fieldService.service.CCGSyncService;
import com.cci.kangdao.fieldService.service.FieldBaseService;
import com.cci.kangdao.fieldService.service.FieldServiceChannelService;
import com.cci.kangdao.fieldService.service.FieldServiceNoticeService;
import com.cci.kangdao.fieldService.service.FieldServiceWorkOrderFlowService;
import com.cci.kangdao.fieldService.service.SyncProcessor;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.JsonUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.RequestContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/12/31
 */
@Service
public class CCGSyncServiceImpl extends FieldBaseService implements CCGSyncService {

    private static final Logger logger = Logger.getLogger(CCGSyncService.class);


    private static final String SMS_CONTENT = "您有新的工单，工单编号%s，请及时处理";

    @Resource
    private FieldServiceItemDao fieldServiceItemDao;

    @Resource
    private FieldServiceTemplateDao fieldServiceTemplateDao;

    @Resource
    private FieldServiceTemplateSectionDao fieldServiceTemplateSectionDao;

    @Resource
    private FieldServiceTemplateParamDao fieldServiceTemplateParamDao;

    @Resource
    private FieldServiceTemplatePickParamDao fieldServiceTemplatePickParamDao;

    @Resource
    private FieldServiceTemplateStandardDao fieldServiceTemplateStandardDao;

    @Resource
    private FieldServiceEngineerDao fieldServiceEngineerDao;

    @Resource
    private FieldServiceWorkOrderDao fieldServiceWorkOrderDao;

    @Resource
    private UserTDao userTDao;

    @Resource
    private FieldServiceUpgradeDao fieldServiceUpgradeDao;

    @Resource
    private FieldServiceEvaluationDao fieldServiceEvaluationDao;

    @Resource
    private FieldServiceWorkOrderFlowService fieldServiceWorkOrderFlowService;

    @Resource
    private FieldServiceChannelService fieldServiceChannelService;

    @Resource
    private SyncProcessor syncProcessor;

    @Resource
    private FieldServiceNoticeService fieldServiceNoticeService;

    @Override
    @Transactional
    public void syncServiceItemFromCrm(FieldServiceItem item) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        if (null != item) {
            String crmNo = item.getCrmItemNo();
            if (item.getRequestStatus().equals(1)) {
                Long itemId;
                // 通过crmNo获取Item
                FieldServiceItem dataItem = fieldServiceItemDao.selectByChannelAndCrmNo(item.getChannel(), crmNo);
                if (null == dataItem) {
                    // 生成item编号
                    String appNo = getItemNo();
                    item.setItemNo(appNo);
                    item.setCreator(item.getChannel());
                    item.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    //新增
                    fieldServiceItemDao.insertSelective(item);
                    itemId = item.getId();
                    // 返回工单编号响应
                    Map<String, Object> data = new HashMap<>(1);
                    data.put("APPItemNo", appNo);
                    ParameterTool.writeSuccessResponse(response, data);
                } else {
                    // 存在item数据，更新数据
                    FieldServiceItem newItem = BeanUtils.convert(item, FieldServiceItem.class);
                    itemId = dataItem.getId();
                    newItem.setId(dataItem.getId());
                    newItem.setUpdator(item.getChannel());
                    newItem.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                    // 修改
                    fieldServiceItemDao.updateByPrimaryKeySelective(newItem);
                    Map<String, Object> data = new HashMap<>(1);
                    data.put("APPItemNo", dataItem.getItemNo());
                    ParameterTool.writeSuccessResponse(response, data);
                }
                // 当取消/关闭item时，删除对应存在的异常重试信息
                if (FieldServiceItemStatus.CANCELLED.getCode().equals(item.getStatus())
                        || FieldServiceItemStatus.CLOSED.getCode().equals(item.getStatus())) {
                    syncProcessor.removeRetry(itemId);
                }
            } else if (item.getRequestStatus().equals(2)) {
                // 理论上crm不会做删除逻辑，此处代码仅保留
                FieldServiceItem dataItem = fieldServiceItemDao.selectByChannelAndCrmNo(item.getChannel(), crmNo);
                if (null != dataItem) {
                    dataItem.setFlag(-1);
                    fieldServiceItemDao.updateByPrimaryKeySelective(dataItem);
                    ParameterTool.writeSuccessResponse(response);
                    syncProcessor.removeRetry(dataItem.getId());
                } else {
                    // 删除不存在数据，记录异常
                    ParameterTool.writeResponse(response, 1, "服务信息不存在，无法删除");
                }
            } else {
                ParameterTool.writeResponse(response, 1, "未知requestStatus:" + item.getRequestStatus());
                // 未知请求类型
            }
        } else {
            ParameterTool.writeResponse(response, 1, "ITEM列表为空");
        }
    }

    @Override
    @Transactional
    public void syncServiceTemplateFromCrm(FieldServiceTemplate template) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        String crmNo = template.getCrmNo();
        // 查询template信息
        FieldServiceTemplate dataTemplate = fieldServiceTemplateDao.selectByChannelAndNo(template.getChannel(), crmNo);
        String currentDate = LocalTimeUtil.getRealCurrentTime();
        if (null == dataTemplate) {
            if (FieldServiceDisplay.INVALID.getCode().equals(template.getDisplay())) {
                // 同步无效模板，记录异常
                logger.warn("直接同步无效模板，可能存在异常" + JsonUtils.toJsonString(template));
            }
            // 保存模板数据
            template.setCreator(template.getChannel());
            template.setCreateTime(currentDate);
            template.setFlag(getFlag(template.getDisplay()));
            fieldServiceTemplateDao.insertSelective(template);
            //  片段数据不为空，保存片段数据
            if (CollectionUtils.isNotEmpty(template.getSectionList())) {
                for (FieldServiceTemplateSection section : template.getSectionList()) {
                    // 插入片段数据
                    insertSection(section, template, currentDate);
                }
            }
        } else {
            // 模板数据存在，更新模板数据
            template.setId(dataTemplate.getId());
            template.setUpdator(template.getChannel());
            template.setUpdateTime(currentDate);
            template.setFlag(getFlag(template.getDisplay()));
            fieldServiceTemplateDao.updateByPrimaryKeySelective(template);
            // 片段数据存在，遍历片段数据
            if (CollectionUtils.isNotEmpty(template.getSectionList())) {
                for (FieldServiceTemplateSection section : template.getSectionList()) {
                    // 查询片段数据
                    FieldServiceTemplateSection dataSection = fieldServiceTemplateSectionDao.select(template.getId(), section.getCrmNo());
                    if (null == dataSection) {
                        // 插入片段数据
                        insertSection(section, template, currentDate);
                    } else {
                        // 更新片段数据
                        section.setId(dataSection.getId());
                        section.setUpdator(template.getChannel());
                        section.setUpdateTime(currentDate);
                        fieldServiceTemplateSectionDao.updateByPrimaryKeySelective(section);
                        // 参数数据不为空，遍历参数数据
                        if (CollectionUtils.isNotEmpty(section.getParamList())) {
                            for (FieldServiceTemplateParam param : section.getParamList()) {
                                // 查询参数数据是否存在
                                FieldServiceTemplateParam dataParam = fieldServiceTemplateParamDao.selectBySectionAndKey(section.getId(), param.getParamKey());
                                if (null == dataParam) {
                                    // 插入模板数据
                                    insertParam(param, section.getId(), template.getChannel(), currentDate);
                                    // 插入下拉选框参数值
                                    insertPickParam(template.getChannel(), param.getId(), currentDate, param.getParamValueList());
                                } else {
                                    // 更新参数数据
                                    param.setId(dataParam.getId());
                                    param.setSectionId(section.getId());
                                    // TODO: 2020/2/5 这里是否可以通过数据算出标记，通过标记减少查询可能，需要重点考虑数据库锁问题
                                    if (CollectionUtils.isNotEmpty(param.getParamValueList())) {
                                        param.setPickList(JsonUtils.toJsonString(param.getParamValueList()));
                                    }
                                    param.setUpdator(template.getChannel());
                                    param.setUpdateTime(currentDate);
                                    param.setFlag(getFlag(param.getDisplay()));
                                    fieldServiceTemplateParamDao.updateByPrimaryKeySelective(param);
                                    // 处理下拉列表数据
                                    mergePickParam(template.getChannel(), param.getId(), currentDate, param.getParamValueList());
                                }
                            }
                        } else {
                            // 删除下属param，理论上不应该有
                            logger.warn("同步Section中不存在ParamList，可能存在问题" + JsonUtils.toJsonString(section));
                        }
                    }
                }
            }
        }
        ParameterTool.writeSuccessResponse(response);
    }

    /**
     * 插入模板片段+参数
     *
     * @param section     片段数据
     * @param template    模板数据
     * @param currentDate 当前时间
     */
    private void insertSection(FieldServiceTemplateSection section, FieldServiceTemplate template, String currentDate) {
        // 插入片段
        section.setTemplateId(template.getId());
        section.setCreator(template.getChannel());
        section.setCreateTime(currentDate);
        section.setFlag(getFlag(section.getDisplay()));
        fieldServiceTemplateSectionDao.insertSelective(section);
        if (CollectionUtils.isNotEmpty(section.getParamList())) {
            for (FieldServiceTemplateParam param : section.getParamList()) {
                // 插入参数
                insertParam(param, section.getId(), template.getChannel(), currentDate);
                // 插入下拉选框参数值
                insertPickParam(template.getChannel(), param.getId(), currentDate, param.getParamValueList());
            }
        }
    }

    /**
     * 插入参数数据
     *
     * @param param       参数信息
     * @param sectionId   片段ID
     * @param channel     渠道
     * @param currentDate 当前时间
     */
    private void insertParam(FieldServiceTemplateParam param, Long sectionId, Long channel, String currentDate) {
        param.setSectionId(sectionId);
        if (CollectionUtils.isNotEmpty(param.getParamValueList())) {
            param.setPickList(JsonUtils.toJsonString(param.getParamValueList()));
        }
        param.setCreator(channel);
        param.setCreateTime(currentDate);
        param.setFlag(getFlag(param.getDisplay()));
        fieldServiceTemplateParamDao.insertSelective(param);
    }

    // 插入下拉选框，此处只负责插入逻辑
    private void insertPickParam(Long channel, Long paramId, String currentDate, List<FieldServiceTemplatePickParam> pickParams) {
        // 插入下拉选框参数值
        if (CollectionUtils.isNotEmpty(pickParams)) {
            for (FieldServiceTemplatePickParam pickParam : pickParams) {
                pickParam.setParamId(paramId);
                pickParam.setCreator(channel);
                pickParam.setCreateTime(currentDate);
                pickParam.setFlag(getFlag(pickParam.getIsActive()));
                fieldServiceTemplatePickParamDao.insertSelective(pickParam);
            }
        }
    }

    // 处理下拉选框，此处包含插入和修改逻辑
    private void mergePickParam(Long channel, Long paramId, String currentDate, List<FieldServiceTemplatePickParam> pickParams) {
        // 插入下拉选框参数值
        if (CollectionUtils.isNotEmpty(pickParams)) {
            for (FieldServiceTemplatePickParam pickParam : pickParams) {
                FieldServiceTemplatePickParam dataParam = fieldServiceTemplatePickParamDao.selectByParamAndKey(paramId, pickParam.getPickListKey());
                if (null == dataParam) {
                    pickParam.setParamId(paramId);
                    pickParam.setCreator(channel);
                    pickParam.setCreateTime(currentDate);
                    pickParam.setFlag(getFlag(pickParam.getIsActive()));
                    fieldServiceTemplatePickParamDao.insertSelective(pickParam);
                } else {
                    dataParam.setPickListValue(pickParam.getPickListValue());
                    dataParam.setFlag(getFlag(pickParam.getIsActive()));
                    dataParam.setUpdator(channel);
                    dataParam.setUpdateTime(currentDate);
                    fieldServiceTemplatePickParamDao.updateByPrimaryKeySelective(dataParam);
                }
            }
        }
    }

    private Integer getFlag(String display) {
        if (FieldServiceDisplay.INVALID.getCode().equals(display)) {
            return -1;
        }
        return 0;
    }

    @Override
    public void syncTemplateStandardFromCrm(Long channel, List<FieldServiceTemplateStandard> standardList) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        if (CollectionUtils.isNotEmpty(standardList)) {
            for (FieldServiceTemplateStandard standard : standardList) {
                if (FieldServiceRequestStatus.EDIT.getCode().equals(standard.getRequestStatus())) {
                    FieldServiceTemplateStandard dataStandard = fieldServiceTemplateStandardDao.select(channel, standard.getStandardKey());
                    if (null == dataStandard) {
                        standard.setChannel(channel);
                        standard.setCreator(channel);
                        standard.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceTemplateStandardDao.insertSelective(standard);
                    } else {
                        standard.setId(dataStandard.getId());
                        standard.setUpdator(channel);
                        standard.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceTemplateStandardDao.updateByPrimaryKeySelective(standard);
                    }
                } else {
                    FieldServiceTemplateStandard dataStandard = fieldServiceTemplateStandardDao.select(channel, standard.getStandardKey());
                    if (null != dataStandard) {
                        dataStandard.setDeleter(channel);
                        dataStandard.setDeleteTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceTemplateStandardDao.updateByPrimaryKeySelective(dataStandard);
                    }
                }
            }
            ParameterTool.writeSuccessResponse(response);
        } else {
            ParameterTool.writeResponse(response, 1, "标准值列表为空");
        }
    }

    @Override
    @Transactional
    public void syncEngineerFromCrm(Long channel, List<FieldServiceEngineer> engineerList) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        if (CollectionUtils.isNotEmpty(engineerList)) {
            for (FieldServiceEngineer engineer : engineerList) {
                FieldServiceEngineer dataEngineer = fieldServiceEngineerDao.selectEngineer(channel, engineer.getEmployeeNum());
                if (FieldServiceRequestStatus.EDIT.getCode().equals(engineer.getRequestStatus())) {
                    if (null == dataEngineer) {
                        engineer.setChannel(channel);
                        engineer.setCreator(channel);
                        engineer.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceEngineerDao.insertSelective(engineer);
                    } else {
                        engineer.setId(dataEngineer.getId());
                        engineer.setUpdator(channel);
                        engineer.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceEngineerDao.updateByPrimaryKeySelective(engineer);
                    }
                } else {
                    if (null != dataEngineer) {
                        engineer.setId(dataEngineer.getId());
                        engineer.setFlag(-1);
                        engineer.setDeleter(channel);
                        engineer.setDeleteTime(LocalTimeUtil.getRealCurrentTime());
                        fieldServiceEngineerDao.updateByPrimaryKeySelective(engineer);
                    } else {
                        logger.error("工程师信息不存在，无法删除" + JsonUtils.toJsonString(engineer));
                    }
                }
            }
            ParameterTool.writeSuccessResponse(response);
        } else {
            ParameterTool.writeResponse(response, 1, "工程师列表为空");
        }
    }

    @Override
    public void syncWorkOrderFromCrm(FieldServiceWorkOrder workOrder) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        // 拼装item信息
        FieldServiceItem fieldServiceItem = fieldServiceItemDao.selectByChannelAndCrmNo(workOrder.getChannel(), workOrder.getCrmItemNo());
        if (null == fieldServiceItem) {
            ParameterTool.writeResponse(response, 1, "当前渠道不存在此编号服务：" + workOrder.getCrmItemNo());
            return;
        }
        String wwid = workOrder.getEngineerWWID();
        UserT userT = userTDao.getUserByEmployeeNum(wwid);
        // 该用户暂未注册
        if (null == userT) {
            ParameterTool.writeResponse(response, 1, "当前工程师暂时未注册APP，暂时无法报单，工程师WWID：" + workOrder.getEngineerWWID());
            return;
        } else {
            Long channel = fieldServiceChannelService.getChannel(userT);
            // 该用户权限不对应
            if (!NumberUtils.isPositiveNum(channel)) {
                ParameterTool.writeResponse(response, 1, "当前工程师无相关权限，暂时无法报单，工程师WWID：" + workOrder.getEngineerWWID());
                return;
            } else {
                workOrder.setEngineerId(userT.getId());
            }
        }

        // 将派工单所属item置为处理中
        fieldServiceItem.setStatus(FieldServiceItemStatus.IN_PROCESS.getCode());
        fieldServiceItem.setUpdator(workOrder.getChannel());
        fieldServiceItemDao.updateByPrimaryKeySelective(fieldServiceItem);

        // 拼装WO信息
        workOrder.setItemId(fieldServiceItem.getId());
        String workOrderNo = getOrderNo();
        workOrder.setWorkOrderNo(workOrderNo);
        // 此处CRM状态为处理中，而APP状态为待处理
        workOrder.setStatus(FieldServiceWorkOrderStatus.NEW.getCode());
        workOrder.setCreator(workOrder.getChannel());
        workOrder.setCreateTime(LocalTimeUtil.getRealCurrentTime());
        fieldServiceWorkOrderDao.insertSelective(workOrder);
        fieldServiceWorkOrderFlowService.saveWorkOrderFlow(WorkOrderFlowProcessorType.CRM_SYSTEM, workOrder, workOrder.getChannel(), "CRM指派工单");
        // 发送短信通知
        //String smsContent = String.format(SMS_CONTENT,workOrderNo);
        //fieldServiceNoticeService.sendSMS(workOrder.getId(),userT.getId(),userT.getPhone(),smsContent,workOrder.getChannel());

        CFWOThreadLocal.set(workOrder.getId());

        // 响应前端
        Map<String, Object> data = new HashMap<>(1);
        data.put("APPWorkOrderNo", workOrderNo);
        ParameterTool.writeSuccessResponse(response, data);
    }

    @Override
    public void syncOrderStatusFromCrm(SyncFieldServiceOrderStatus orderStatus) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        FieldServiceWorkOrder workOrder = fieldServiceWorkOrderDao.selectByChannelAndCrmNo(orderStatus.getChannel(), orderStatus.getCrmOrderNo());
        if (null != workOrder) {
            // 存在解决方案，并且工单为处理中或取消状态，尝试更新升级信息
            if (StringUtils.isNotEmpty(orderStatus.getMessage()) &&
                    (FieldServiceWorkOrderStatus.IN_PROCESS.getCode().equals(orderStatus.getStatus()) || FieldServiceWorkOrderStatus.CANCELLED.getCode().equals(orderStatus.getStatus()))) {
                // 获取升级信息
                FieldServiceUpgrade dataUpgrade = fieldServiceUpgradeDao.selectLastByOrderId(workOrder.getId());
                // 升级不为空，且在处理中，完善升级方案
                if (null != dataUpgrade && FieldServiceUpgradeStatus.ON_PROCESS.getCode().equals(dataUpgrade.getStatus())) {
                    dataUpgrade.setStatus(FieldServiceUpgradeStatus.FINISH.getCode());
                    dataUpgrade.setSolution(orderStatus.getMessage());
                    dataUpgrade.setUpdator(orderStatus.getChannel());
                    dataUpgrade.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                    fieldServiceUpgradeDao.updateByPrimaryKeySelective(dataUpgrade);
                    fieldServiceWorkOrderFlowService.saveWorkOrderFlow(WorkOrderFlowProcessorType.CRM_SYSTEM, workOrder, workOrder.getChannel(), "CRM同步工单升级解决方案");
                }
            }
            // 如果是工单取消
            if(FieldServiceWorkOrderStatus.CANCELLED.getCode().equals(orderStatus.getStatus())){
                // 如果为开放任务，则将ITEM完成次数减1
                FieldServiceItem item = fieldServiceItemDao.selectByPrimaryKey(workOrder.getItemId());
                if(item.getType().equals(FieldServiceItemType.OPEN.getCode())){
                    Integer newCompleteCount = item.getCompleteCount() -1;
                    item.setCompleteCount(NumberUtils.isPositiveNum(newCompleteCount) ? newCompleteCount : 0);
                    item.setUpdator(orderStatus.getChannel());
                    item.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                    fieldServiceItemDao.updateByPrimaryKeySelective(item);
                }
            }
            workOrder.setStatus(orderStatus.getStatus());
            workOrder.setUpdator(orderStatus.getChannel());
            workOrder.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
            fieldServiceWorkOrderDao.updateByPrimaryKeySelective(workOrder);
            fieldServiceWorkOrderFlowService.saveWorkOrderFlow(WorkOrderFlowProcessorType.CRM_SYSTEM, workOrder, workOrder.getChannel(), "CRM修改工单状态");
            ParameterTool.writeSuccessResponse(response);
        } else {
            ParameterTool.writeResponse(response, 1, "工单信息不存在");
        }
    }

    @Override
    public void syncOrderEvaluation(FieldServiceEvaluation fieldServiceEvaluation) {
        HttpServletResponse response = RequestContextHolder.getResponse();
        FieldServiceWorkOrder workOrder = fieldServiceWorkOrderDao.selectByChannelAndCrmNo(fieldServiceEvaluation.getChannel(), fieldServiceEvaluation.getCrmOrderNo());
        if (null != workOrder) {
            if (FieldServiceWorkOrderStatus.CLOSURE_INITIATED.getCode().equals(workOrder.getStatus())) {
                fieldServiceEvaluation.setOrderId(workOrder.getId());
                fieldServiceEvaluation.setCreator(fieldServiceEvaluation.getChannel());
                fieldServiceEvaluation.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                fieldServiceEvaluationDao.insertSelective(fieldServiceEvaluation);
                ParameterTool.writeSuccessResponse(response);
                fieldServiceWorkOrderFlowService.saveWorkOrderFlow(WorkOrderFlowProcessorType.CRM_SYSTEM, workOrder, workOrder.getChannel(), "CRM同步工单评分");
            } else {
                ParameterTool.writeResponse(response, 1, "当前工单状态不允许该同步信息");
            }
        } else {
            ParameterTool.writeResponse(response, 1, "工单信息不存在");
        }
    }


}
